package cn.iocoder.yudao.module.ems.service.warehousing;

import cn.iocoder.yudao.module.ems.convert.warehousing.WarehousingInfoConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.spares.SparesDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.warehousing.WarehousingInfoDO;
import cn.iocoder.yudao.module.ems.dal.mysql.spares.SparesMapper;
import cn.iocoder.yudao.module.ems.dal.mysql.warehousing.WarehousingInfoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.ems.controller.admin.warehousing.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.warehousing.WarehousingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.ems.convert.warehousing.WarehousingConvert;
import cn.iocoder.yudao.module.ems.dal.mysql.warehousing.WarehousingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.*;

/**
 * 入库记录 Service 实现类
 *
 * @author dason
 */
@Service
@Validated
public class WarehousingServiceImpl implements WarehousingService {

    @Resource
    private WarehousingMapper warehousingMapper;

    @Resource
    private WarehousingInfoMapper warehousingInfoMapper;
    @Resource
    private SparesMapper sparesMapper;

    @Override
    public Long createWarehousing(WarehousingCreateReqVO createReqVO) {
        // 插入
        WarehousingDO warehousing = WarehousingConvert.INSTANCE.convert(createReqVO);
        warehousingMapper.insert(warehousing);
        Collection<WarehousingInfoCreateReqVO> InfoList = createReqVO.getWarehousingInfo();
        for (WarehousingInfoCreateReqVO info: InfoList) {
            SparesDO spares = sparesMapper.selectByItemNo(info.getItemNo());
            if (spares == null) {
                throw exception(SPARES_ITEMNO_NOTEXISTS);
            }
            info.setWarehousingId(warehousing.getId());
            warehousingInfoMapper.insert(WarehousingInfoConvert.INSTANCE.convert4(info));
            sparesMapper.updateAmountByInfo(spares, info.getNumber());
        }
        // 返回
        return warehousing.getId();
    }

    @Override
    public void updateWarehousing(WarehousingUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateWarehousingExists(updateReqVO.getId());
        // 更新
        WarehousingDO updateObj = WarehousingConvert.INSTANCE.convert(updateReqVO);
        // 入库信息
        Collection<WarehousingInfoRespVO> InfoList = updateReqVO.getWarehousingInfo();
        for (WarehousingInfoRespVO info: InfoList) {
            SparesDO spares = sparesMapper.selectByItemNo(info.getItemNo());
            if (spares == null) {
                throw exception(SPARES_ITEMNO_NOTEXISTS);
            }
            info.setWarehousingId(updateReqVO.getId());
            if(info.getId() != null) {
                WarehousingInfoDO rawInfo = warehousingInfoMapper.selectById(info.getId());
                // 物料编号相等
                if(Objects.equals(info.getItemNo(), rawInfo.getItemNo())) {
                    if(!Objects.equals(info.getNumber(), rawInfo.getNumber())) {
                        sparesMapper.updateAmountByInfo(spares, info.getNumber() - rawInfo.getNumber());
                    }
                    warehousingInfoMapper.updateById(WarehousingInfoConvert.INSTANCE.convert3(info));
                } else {
                    SparesDO other = sparesMapper.selectByItemNo(rawInfo.getItemNo());
                    sparesMapper.updateAmountByInfo(other, -rawInfo.getNumber());
                    sparesMapper.updateAmountByInfo(spares, info.getNumber());
                }
            } else {
                warehousingInfoMapper.insert(WarehousingInfoConvert.INSTANCE.convert3(info));
                sparesMapper.updateAmountByInfo(spares, info.getNumber());
            }
        }
        warehousingMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehousing(Long id) {
        // 校验存在
        this.validateWarehousingExists(id);
        WarehousingDO w = warehousingMapper.selectById(id);
        List<WarehousingInfoDO> infoList = warehousingInfoMapper.selectList(id);
        for(WarehousingInfoDO info : infoList) {
            SparesDO spares = sparesMapper.selectByItemNo(info.getItemNo());
            sparesMapper.updateAmountByInfo(spares, -info.getNumber());
            warehousingInfoMapper.deleteById(info.getId());
        }
        // 删除
        warehousingMapper.deleteById(id);
    }

    private void validateWarehousingExists(Long id) {
        if (warehousingMapper.selectById(id) == null) {
            throw exception(WAREHOUSING_NOT_EXISTS);
        }
    }

    @Override
    public WarehousingDO getWarehousing(Long id) {
        return warehousingMapper.selectById(id);
    }

    @Override
    public List<WarehousingDO> getWarehousingList(Collection<Long> ids) {
        return warehousingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WarehousingDO> getWarehousingPage(WarehousingPageReqVO pageReqVO) {
        return warehousingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehousingDO> getWarehousingList(WarehousingExportReqVO exportReqVO) {
        return warehousingMapper.selectList(exportReqVO);
    }

}
